package com.yuncheng.spcyApi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.entity.SpcyJcyRzlb;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SpcyJcyRzlbMapper;
import com.yuncheng.spcyApi.service.ISpcyJcyRzlbService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: 检查员采集认证类别
 * @Author: yuncheng
 * @Date: 2024-01-08
 * @Version: V1.0
 */
@Service
public class SpcyJcyRzlbServiceImpl extends BaseServiceImpl<SpcyJcyRzlbMapper, SpcyJcyRzlb> implements ISpcyJcyRzlbService {

    @Resource
    private SpcyJcyRzlbMapper spcyJcyRzlbMapper;

    @Override
    public void updateLb(HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException {
        String id = buffer.get("id");

        SpcyJcyRzlb Update = spcyJcyRzlbMapper.selectById(id);
        if (Update == null){
            throw new BusinessException("当前基本信息数据不存在，请重试");
        }
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(Update, buffer);

        Update.setCreateTime(new Date());
        Update.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        spcyJcyRzlbMapper.updateById(Update);
    }

    @Override
    public List<SpcyJcyRzlb> selectLbList(String userid, String flxjhid, boolean isSelect) {
        LambdaQueryWrapper<SpcyJcyRzlb> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcyJcyRzlb::getFuserid, userid);
        queryWrapper.eq(SpcyJcyRzlb::getFlxjhid, flxjhid);
        if (isSelect){
            queryWrapper.eq(SpcyJcyRzlb::getFxzzt, "1");
        }
        queryWrapper.orderByAsc(SpcyJcyRzlb::getForder);
        queryWrapper.orderByAsc(SpcyJcyRzlb::getCreateTime);
        return spcyJcyRzlbMapper.selectList(queryWrapper);
    }
}
